package com.ygp.basic;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;

import static com.ygp.utils.Sleeper.sleep;


@Slf4j(topic = "c.Test14")
public class TestInterrupted3 {

    private static void test4() {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                log.debug("park...");
                LockSupport.park();
                log.debug("打断状态：{}", Thread.interrupted());
            }
        });
        t1.start();
        sleep(1);
        t1.interrupt();
    }

//    private static void test3() throws InterruptedException {
//        Thread t1 = new Thread(() -> {
//            log.debug("park...");
//            LockSupport.park();
//            log.debug("unpark...");
//            log.debug("打断状态：{}", Thread.currentThread().isInterrupted());
//        }, "t1");
//        t1.start();
//
//        sleep(1);
//        t1.interrupt();
//
//    }

    private static void test1() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("park...");
            LockSupport.park();
            log.debug("do something....");// 不会再执行
        }, "t1");
        t1.start();
        t1.join();
    }


    private static void test2() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("park...");
            LockSupport.park();
            log.debug("打断状态：{}", Thread.currentThread().isInterrupted());
            log.debug("do something....");// 会继续执行
        }, "t1");
        t1.start();
        t1.interrupt();
        t1.join();
    }

    private static void test3() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("park...");
            LockSupport.park();
            log.debug("打断状态：{}", Thread.currentThread().isInterrupted());
            log.debug("do something....");// 会继续执行
            LockSupport.park(); // 这里就不会再停止
            log.debug("do something again....");
        }, "t1");
        t1.start();
        t1.interrupt();
        t1.join();
    }

    private static void test5() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("park...");
            LockSupport.park();
            log.debug("打断状态：{}", Thread.interrupted());// 重置标记位
            log.debug("do something....");// 会继续执行
            LockSupport.park(); // 这里会再停止
            log.debug("do something again....");
        }, "t1");
        t1.start();
        t1.interrupt();
        t1.join();
    }

    private static void test6() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("park...");
            LockSupport.park();
            log.debug("打断标志位:{}", Thread.currentThread().isInterrupted());
        }, "t1");
        t1.start();
        t1.interrupt();
        // 重置标记位
        log.debug("打断标志位:{}", t1.isInterrupted());
        t1.join();
    }


    public static void main(String[] args) throws InterruptedException {
//        test4();
//        test5();
        test6();
    }
}
